head = [1,2,3,4,5]
k = 2

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
#1->2->3->4->5
# head=ListNode(0,None)
# head.next=ListNode(2,None)
# head.next.next=ListNode(3,None)
# head.next.next.next=ListNode(4,None)
# head.next.next.next.next=ListNode(5,None)
head=ListNode(0,None)
head.next=ListNode(1,None)
head.next.next=ListNode(2,None)
def rotateRight(head, k):
    if head==None:
        #空链表的情况
        return None
    #先计算整个链表的长度
    head_root=head
    linkLen=0
    while head_root!=None:
        linkLen+=1
        head_root=head_root.next
    targetLoc=linkLen-(k%linkLen)
    #如果要旋转的位置正好是最后一个元素位置，直接返回原链表即可
    if targetLoc==linkLen:
        return head
    head_root=head
    cur_index=1
    #找到要旋转位置的前一个位置
    while cur_index<targetLoc and head_root!=None:
        head_root=head_root.next
        cur_index+=1
    ans = head_root.next
    head_root.next=None
    ans_process=ans
    while ans_process.next!=None:
        ans_process=ans_process.next
    ans_process.next=head
    while ans!=None:
        print(ans.val)
        ans=ans.next
rotateRight(head,4)